package hc.j2me.ui;

import javax.microedition.lcdui.game.Sprite;

import hc.core.ConditionWatcher;
import hc.core.IWatcher;
import hc.core.L;
import hc.core.util.LogManager;

public class SpriteMover implements IWatcher{
	final static int STEP_NUM = 5;//移出或移出时，总共产生的过程动画数
	int[] stepX = new int[STEP_NUM], stepY = new int[STEP_NUM];
	int currStep = 0;
	public SpritesLoc spriteloc;
	private boolean isAddWatchSelf = false;
	
	public boolean isAddWatch(){
		return isAddWatchSelf;
	}
	
	public void flipMove(){
		if(isAddWatchSelf){
			return;
		}
		
		isAddWatchSelf = true;

		spriteloc.isMoveIn = !spriteloc.isMoveIn;
		spriteloc.moveY *= -1;
		spriteloc.moveX *= -1;
		
		setPara(spriteloc);
	}
	
	private void initLoc(){
		final int spriteNum = spriteloc.sprites.length;
		if(spriteloc.isMoveIn){
			for (int i = 0; i < spriteNum; i++) {
				Object sprites = spriteloc.sprites[i];
				if(sprites == null){
					continue;
				}else if(sprites instanceof Sprite[]){
					Sprite[] ss = (Sprite[])sprites;
					for (int j = 0; j < ss.length; j++) {
						initSprite(ss[j]);
					}
				}else{
					initSprite(sprites);
				}
			}
		}
		
	}

	private void initSprite(final Object obj) {
		if(obj != null){
			Sprite _sprite = (Sprite)obj;
			_sprite.setPosition(
				_sprite.getX() - spriteloc.moveX, 
				_sprite.getY() - spriteloc.moveY);
		}
	}
	
	private void mvSprite(final Object obj) {
		if(obj != null){
			Sprite _sprite = (Sprite)obj;
			_sprite.setPosition(_sprite.getX() + stepX[currStep], _sprite.getY() + stepY[currStep]);
		}
	}
	
	public boolean watch() {
		final int spriteNum = spriteloc.sprites.length;

		if(currStep == 0){
			initLoc();
		}
		
		synchronized (spriteloc.lockObj) {
			for (int i = 0; i < spriteNum; i++) {
				Object sprites = spriteloc.sprites[i];
				if(sprites == null){
					continue;
				}else if(sprites instanceof Sprite[]){
					Sprite[] ss = (Sprite[])sprites;
					for (int j = 0; j < ss.length; j++) {
						mvSprite(ss[j]);
					}
				}else{
					mvSprite(sprites);
				}
			}
			
			if(spriteloc.isMoveIn && currStep == 0){
				setSpritesVisiable(true);
			}
			
			spriteloc.hccanvas.refreshScreenDirect();
			
//			System.out.println("Now x:" + spriteloc.sprites[0].getX() + ", y:" + spriteloc.sprites[0].getY());

			currStep++;
			
			if(currStep == STEP_NUM){
				if(spriteloc.isMoveIn == false){
					setSpritesVisiable(false);
					
					//恢复到移入后的坐标
					for (int i = 0; i < spriteNum; i++) {
						Object sprites = spriteloc.sprites[i];
						if(sprites == null){
							continue;
						}else if(sprites instanceof Sprite[]){
							Sprite[] ss = (Sprite[])sprites;
							for (int j = 0; j < ss.length; j++) {
								endMvSprite(ss[j]);
							}
						}else{
							endMvSprite(sprites);
						}
					}
				}
				
				isAddWatchSelf = false;
				return true;
			}
		}
		
		return false;
	}
	
	private void endMvSprite(final Object obj){
		if(obj != null){
			Sprite _sprite = (Sprite)obj;
			_sprite.setPosition(_sprite.getX() - spriteloc.moveX, _sprite.getY() - spriteloc.moveY);
		}
	}
	
	private void setSpriteVisible(final Object obj, boolean visiable){
		if(obj != null){
			Sprite _sprite = (Sprite)obj;
			_sprite.setVisible(visiable);
		}
	}

	private void setSpritesVisiable(boolean visiable) {
//		System.out.println("setSpritesVisiable : " + visiable);
		
		final int spriteNum = spriteloc.sprites.length;
		for (int i = 0; i < spriteNum; i++) {
			Object sprites = spriteloc.sprites[i];
			if(sprites == null){
				continue;
			}else if(sprites instanceof Sprite[]){
				Sprite[] ss = (Sprite[])sprites;
				for (int j = 0; j < ss.length; j++) {
					setSpriteVisible(ss[j], visiable);
				}
			}else{
				setSpriteVisible(sprites, visiable);
			}
		}
	}

	public void setPara(Object p) {
		this.spriteloc = (SpritesLoc)p;
		currStep = 0;
		
		if(spriteloc.moveX == 0){
			int doubleTwo = 1;
			int sumDoubleTwo = 0;
			for (int i = STEP_NUM - 1; i >= 0; i--) {
				doubleTwo *= 2;
				stepY[i] = spriteloc.moveY / (doubleTwo);
				sumDoubleTwo += stepY[i];
				stepX[i] = 0;
				
//					System.out.println("move x:" + stepX[i] + ", y:" + stepY[i]);
			}
			
			stepY[STEP_NUM - 1] += spriteloc.moveY - sumDoubleTwo;
		}else{
			//moveY == 0
			int doubleTwo = 1;
			int sumDoubleTwo = 0;
			for (int i = STEP_NUM - 1; i >= 0; i--) {
				doubleTwo *= 2;
				stepX[i] = spriteloc.moveX / (doubleTwo);
				sumDoubleTwo += stepX[i];
				stepY[i] = 0;
				
//					System.out.println("move x:" + stepX[i] + ", y:" + stepY[i]);
			}
			
			stepX[STEP_NUM - 1] += spriteloc.moveX - sumDoubleTwo;
		}
		
		ConditionWatcher.addWatcher(this);
	}

	public void cancel() {
	}

	public boolean isNotCancelable() {
		return false;
	}

}
